#!/usr/bin/python
# -*- coding: utf-8 -*-




import threading





from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor

class Chat(LineReceiver):

    def __init__(self):
        self.received=[]


    def connectionMade(self):
        self.sendLine("who are u?")

    def connectionLost(self, reason):
        self.sendLine("bye!")


    def dataReceived(self, data):
        self.received.append(data)
        self.sendLine(data)
        print data

    def lineReceived(self, line):
        self.received.append(line)
        self.sendLine(line)
        print line




class ChatFactory(Factory):

    def __init__(self):
        pass

    def buildProtocol(self, addr):
        return Chat()



class android_websocket:

    def __init__(self,port=7778,autostart=True):
        self.port=port
        self.autostart=autostart
        self.factory=ChatFactory()
        self.__startSocket__()
        if self.autostart:
            self.thread.start()
        
    def __socketInit__(self):
        reactor.listenTCP(self.port, self.factory)
        reactor.run(installSignalHandlers=0)
 
 

    def __startSocket__(self):
        self.thread=threading.Thread(target=self.__socketInit__, args=())























